/* 基于下面提供的代码，完成后续的四个练习 */
const fp = require('lodash/fp');
class Container {
  static of(value) {
    return new Container(value);
  }

  constructor(value) {
    this._value = value;
  }

  map(fn) {
    return Container.of(fn(this._value));
  }
}

class Maybe {
  static of(value) {
    return new Maybe(value);
  }
  isNothing() {
    return this._value === undefined || this._value === null;
  }
  constructor(value) {
    this._value = value;
  }

  map(fn) {
    return this.isNothing() ? this : Maybe.of(fn(this._value));
  }
}

// 练习1
const maybe = Maybe.of([5, 6, 1]);
let ex1 = () => {
  return maybe.map(value => fp.map(fp.add(1))(value));
};
// console.log(ex1());

// 练习2
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so']);
let rx2 = () => {
  return xs.map(value => fp.first(value))._value;
};
// console.log(rx2());

// 练习3
let safeProp = fp.curry((x, o) => Maybe.of(o[x]));
let user = { id: 2, name: 'Albert' };
let ex3 = () => {
  // 方式1
  return fp.compose(fp.first, fp.prop('_value'), safeProp('name'))(user);
  // 方式2
  // return safeProp('name', user).map(v => fp.first(v))._value;
};
// console.log(ex3());

// 练习4
let ex4 = n => {
  return Maybe.of(n).map(v => parseInt(v))._value;
};
// console.log(ex4(4));
// console.log(ex4(null));
